/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "Value = {Value}" )]
  internal sealed class IndexKey : IComparable<IndexKey>, IComparable
  {
    #region Constructor

    internal IndexKey( object owner, DataQueryContext context )
    {
      if( owner == null )
        throw new ArgumentNullException( "owner" );

      if( context == null )
        throw new ArgumentNullException( "context" );

      this.Owner = owner;
      this.Context = context;
    }

    internal IndexKey( object owner, DataQueryContext context, long value )
      : this( owner, context )
    {
      this.Value = value;
    }

    #endregion

    #region Value Property

    internal Nullable<long> Value
    {
      get;
      set;
    }

    #endregion

    #region Context Property

    internal DataQueryContext Context
    {
      get;
      private set;
    }

    #endregion

    #region Private Owner Property

    private object Owner
    {
      get;
      set;
    }

    #endregion

    #region IComparable<IndexKey> Members

    public int CompareTo( IndexKey key )
    {
      if( ( key == null ) || ( !object.ReferenceEquals( key.Owner, this.Owner ) ) )
        throw new DataInternalException( "Cannot compare the keys that are from different data sources." );

      if( object.ReferenceEquals( key, this ) )
        return 0;

      bool reversedSortOrder;
      if( !key.Context.IsSimilarContext( this.Context, out reversedSortOrder ) )
        throw new DataInternalException( "Cannot compare the keys that have been generated in different context." );

      if( ( this.Value != null ) && ( key.Value != null ) )
      {
        long firstIndex = this.Value.Value;
        long secondIndex = key.Value.Value;

        // Convert one index to make them comparable.
        if( reversedSortOrder )
        {
          long dataSourceSize = Math.Max( this.Context.DataSourceSize, key.Context.DataSourceSize );
          secondIndex = dataSourceSize - secondIndex - 1;
        }

        return firstIndex.CompareTo( secondIndex );
      }
      else if( this.Value == null )
      {
        return -1;
      }
      else if( key.Value == null )
      {
        return 1;
      }
      else
      {
        // Both keys have an unknown index.  Since unknown indexes must not be 
        // considered equals, we'll sort them out using another criteria.
        return ( this.GetHashCode() <= key.GetHashCode() ) ? -1 : 1;
      }
    }

    #endregion

    #region IComparable Members

    public int CompareTo( object key )
    {
      return this.CompareTo( key as IndexKey );
    }

    #endregion
  }
}
